import requests
from bs4 import BeautifulSoup

from news_2.src.main.utils.file_tool import read_file_contents

"""
 发送http请求获得页面元素
"""


def get_html_text(_url):
    try:
        response = requests.get(_url)

        # 检查响应状态码
        if 200 <= response.status_code < 300:
            print('请求成功:', response.status_code)
            return response.text
        else:
            print('请求失败:', response.status_code)
            return None
    except requests.exceptions.RequestException as e:
        print('请求异常:', e)


"""
 1.根据爬取设置html元素的path
 2.根据html得path获取html元素标签
"""


def get_html_tag(element, root):
    if 'id' in root:
        element = element.find(id=root['id'])
    elif 'attrs' in root and 'tag_name' in root:
        element = element.find(root['tag_name'], attrs=root['attrs'])
    elif 'tag_name' in root:
        element = element.find(root['tag_name'])
    if element and 'children' in root:
        return get_html_tag(element, root['children'])

    return element


"""
 根据提取到html元素提取数据
 1.数据在标签的文本中 HTML.text
 2.数据在标签的属性中   
"""


def get_data_from_label(label, attribute=None, value=None):
    # print(label)
    # print(f"attribute:{attribute}")
    # print(f"value:{value}")
    if label is None:
        return None
    if attribute is not None and attribute != "":
        return label.get(attribute, None)
    if value == 1:
        return label.text
    return None


def get_data_from_label_path(label, path):
    attribute = path['attribute']
    value = path['value']
    return get_data_from_label(label, attribute, value)


def extract_news(key, value):
    # 1.获取html元素
    text = read_file_contents(f"../resources/{key}.txt")
    if text is None or text == "":
        return None
    # 2 判断字符集是否转码
    if "encode" in value and value['encode']:
        try:
            # 需要解码
            print(f"正在解码{value['encode']}---->utf-8")
            text = text.encode(value['encode']).decode("utf-8")
        except Exception as e:
            print(f"解码异常 '{value['encode']}': {e}")
    else:
        pass
        print("无需解码")
    # 3 使用BeautifulSoup解析html
    soup = BeautifulSoup(text, 'html.parser')
    # 4 获取标题的html标签元素并提取数据
    title_tag = get_html_tag(soup, value['title_dict'])
    title_value = get_data_from_label_path(title_tag, value['title_value'])
    # 5 获取标题链接
    title_link = get_data_from_label_path(title_tag, value['title_link'])
    # 6 获取发布时间的htmk标签元素并提取数据
    time_tag = get_html_tag(soup, value['time_dict'])
    time_value = get_data_from_label_path(time_tag, value['time_value'])
    result = (f"标题: {title_value}\n"
              f"地址: {title_link}\n"
              f"发布时间: {time_value}\n")
    return result


def send_msg(msg):
    # 有结果进行发送
    if msg is not None and msg != "":
        # self.bot.send_text(msg)
        print(f"机器人发送消息：\n{msg}")


def extract_news_bat(json_data):
    for i, (key, value) in enumerate(json_data.items()):
        print("-" * 50)
        if "is_open" in value and value['is_open'] == 1:
            print(f"正在抓取{key} ")
            result = extract_news(key, value)
            # 发送消息
            send_msg(result)
        else:
            continue
